const gcd = (x, y) => y ? gcd(y, x % y) : x;

/**
 * 旋转数组
 * https://leetcode-cn.com/problems/rotate-array/
 * @param {number[]} nums
 * @param {number} k
 * @return {void} Do not return anything, modify nums in-place instead.
 */
var rotate = function (nums, k) {
  const n = nums.length;
  k = k % n;
  let count = gcd(k, n);
  for (let start = 0; start < count; ++start) {
    let current = start;
    let prev = nums[start];
    do {
      const next = (current + k) % n;
      const temp = nums[next];
      nums[next] = prev;
      prev = temp;
      current = next;
    } while (start !== current);
  }
};




let nums = [1, 2, 3, 4, 5, 6, 7], k = 2

rotate(nums, k)
console.log(nums)

//================v4要点总结===============
/**
 * 1.该种方式为题解的第二种，求取k和 nums.length 的最大公约数，作为遍历次数
 * https://leetcode-cn.com/problems/rotate-array/solution/xuan-zhuan-shu-zu-by-leetcode-solution-nipk/
 */